package com.itheima.controller;


import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user != null) {
            String username = user.getUsername();
            return  Result.success(username,MessageConstant.GET_USERNAME_SUCCESS);
        }

        return  Result.error( MessageConstant.GET_USERNAME_FAIL);
    }

    /**
     * 通过用户名查找对应的侧边栏
     *
     * @return
     */
    @RequestMapping("/findSidebar")
    public Result findSidebar() {
        String username = null;
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            username = user.getUsername();
        }
        try {
            List<Map> data = userService.findMeauByUsername(username);
            return  Result.success(data, MessageConstant.GET_USERPERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.GET_USERPERMISSION_FAIL);
        }

    }


    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }


    //新增一个用户
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        String password = user.getPassword();
        com.itheima.pojo.User u = new com.itheima.pojo.User();
        u.setPassword(bCryptPasswordEncoder.encode(password));
        u.setUsername(user.getUsername());
        u.setGender(user.getGender());
        u.setStation(user.getStation());
        u.setTelephone(user.getTelephone());
        u.setBirthday(user.getBirthday());
        u.setRemark(user.getRemark());
        userService.add(u, roleIds);
        return Result.success(MessageConstant.ADD_USER_SUCCESS);
    }


    //编辑一个用户,用于用户的基本数据的回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        com.itheima.pojo.User data = userService.findById(id);
        return Result.success(data, MessageConstant.QUERY_USER_SUCCESS);
    }

    //编辑一个用户,用于查看用户关联了哪些角色
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        List<Integer> data = userService.findRoleIdsByUserId(id);
        return Result.success(data, MessageConstant.QUERY_USER_SUCCESS);
    }

    //编辑一个用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {
        String password = user.getPassword();
        com.itheima.pojo.User u = new com.itheima.pojo.User();
        u.setId(user.getId());
        if (password != null) {
            u.setPassword(bCryptPasswordEncoder.encode(password));
        }
        u.setUsername(user.getUsername());
        u.setGender(user.getGender());
        u.setStation(user.getStation());
        u.setTelephone(user.getTelephone());
        u.setBirthday(user.getBirthday());
        u.setRemark(user.getRemark());

        userService.edit(u, roleIds);
        return Result.success(MessageConstant.EDIT_USER_SUCCESS);

    }

    //删除一个用户
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        userService.delete(id);
        return Result.success(MessageConstant.DELETE_USER_SUCCESS);
    }


}
